<template>
  <div class="reg-container">
    <div class="logo">
      <img src="../../assets/images/logo.png" alt="" />
    </div>
    <div class="form-container">
      <div class="title">
        <img src="../../assets/images/login_title.png" alt="" />
      </div>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-lock"
            placeholder="请输入用户名"
            v-model.number="ruleForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            type="password"
            v-model="ruleForm.password"
            prefix-icon="el-icon-user-solid"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            placeholder="请在此输入密码"
            v-model="ruleForm.repassword"
            prefix-icon="el-icon-lock"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-button
          style="width: 100%"
          type="primary"
          @click="submitForm('ruleForm')"
          >注册</el-button
        >

        <el-link @click="$router.push('/login')">去登陆</el-link>
      </el-form>
    </div>
  </div>
</template>
<script>
import request from '@/utils/request.js'
export default {
  data () {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        password: '',
        repassword: '',
        username: ''
      },
      rules: {
        password: [{ validator: validatePass, trigger: 'blur' }],
        repassword: [{ validator: validatePass2, trigger: 'blur' }],
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }]
      }
    }
  },
  methods: {
    submitForm (formName) {
      /*
      步骤分析：
      1、再次校验
      2、发送请求
      3、成功后===跳转到登录页+提示[注册成功]
      */

      //  再次校验
      this.$refs[formName].validate((valid) => {
        console.log(valid, 77)
        if (valid) {
          //  校验成功==发请求
          // 2、发送请求
          request({
            method: 'post',
            url: '/api/reg',
            data: this.ruleForm
          }).then((res) => {
            console.log(res, 666)
            this.$message({
              message: '恭喜你，这是一条成功消息',
              type: 'success'
            })
            /* 跳转回登录 */
            this.$router.push('/login')
          })
        } else {
          //  校验不成功
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style lang='less'  scoped>
.reg-container {
  height: 100vh;
  // background-color: pink;
  background: url('../../assets/images/login_bg.jpg') no-repeat center / cover;
  position: relative;

  .logo {
    width: 1200px;
    margin: 0 auto;
  }

  .form-container {
    width: 400px;
    height: 335px;
    background-color: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 25px;
    box-sizing: border-box;
    .title {
      text-align: center;
      margin-bottom: 10px;
    }
  }
}
</style>
